#include "CampaignEditForm.h"
#include "ui_CampaignEditForm.h"
#include "CampaignViewDelegate.h"
#include "ItemReference.h"



CampaignEditFormBase::CampaignEditFormBase(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CampaignEditForm)
{
    ui->setupUi(this);
    ui->treeView->setItemDelegate(new CampaignViewDelegate(this));

    addAction(ui->actionAdd_Group);
    addAction(ui->actionAdd_Item);
    addAction(ui->actionAdd_Reference);
    addAction(ui->actionDelete);
}



CampaignEditFormBase::~CampaignEditFormBase()
{
    delete ui;
}



void CampaignEditFormBase::updateData()
{
}



void CampaignEditFormBase::initialize()
{
    ui->treeView->setModel(model());

    ui->treeView->hideColumn(AbstractItem::IdData);
    ui->treeView->hideColumn(AbstractItem::CategoryData);
}



void CampaignEditFormBase::on_actionAdd_Group_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    model->insertRows(model->rowCount(QModelIndex()), 1, QModelIndex());
    ui->treeView->setCurrentIndex(model->index(model->rowCount(QModelIndex())-1,0,QModelIndex()));
}



void CampaignEditFormBase::on_actionAdd_Item_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    QVariant category = index.sibling(index.row(), AbstractItem::CategoryData).data(Qt::EditRole);
    if (!category.isValid()) return;

    QModelIndex parent;
    switch (category.toInt())
    {
    case ItemCategory_Root: return;
    case ItemCategory_Group: { parent = index; break; }
    case ItemCategory_Item:  { parent = index.parent(); break; }
    case ItemCategory_Reference: { parent = index.parent().parent(); break; }
    default: return;
    }

    model->insertRows(model->rowCount(parent), 1, parent);
    ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}



void CampaignEditFormBase::on_actionAdd_Reference_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    QVariant category = index.sibling(index.row(), AbstractItem::CategoryData).data(Qt::EditRole);
    if (!category.isValid()) return;

    QModelIndex parent;
    switch (category.toInt())
    {
    case ItemCategory_Item:         { parent = index; break; }
    case ItemCategory_Reference:    { parent = index.parent(); break; }

    case ItemCategory_Root:         return;
    case ItemCategory_Group:        return;
    default: return;
    }

    model->insertRows(model->rowCount(parent), 1, parent);
    ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}



void CampaignEditFormBase::on_actionDelete_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    model->removeRows(index.row(), 1, index.parent());
    //ui->treeView->setCurrentIndex(model->index(model->rowCount(parent)-1,0,parent));
}


